package com.gjy.sentinel.core.demo;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;

/**
 * 系统保护
 *
 * @author gjy
 * @version 1.0
 * @since 2025-10-15 15:43:07
 */
public class SystemDemo {
    private static final String RESOURCE = "SystemCheck";

    public static boolean tryEnter() {
        Entry entry = null;
        try {
            entry = SphU.entry(RESOURCE);
            System.out.println("[SystemDemo] 通过系统保护检查");
            return true;
        } catch (BlockException be) {
            if (be instanceof SystemBlockException) {
                System.out.println("[SystemDemo] 系统保护阻塞 => " + be.getRule().getLimitApp());
            } else {
                System.out.println("[SystemDemo] 其它阻塞 => " + be.getClass().getSimpleName());
            }
            return false;
        } finally {
            if (entry != null) entry.exit();
        }
    }
}
